package Zoo.view.registerview;

import Zoo.handler.RegisterHandler;

import javax.swing.*;
import java.awt.*;

/**
 * 注册的界面
 */
public class RegisterView extends JFrame /*implements ItemListener */ {
    JLabel nameLabel = new JLabel("用户注册界面", JLabel.CENTER);
    SpringLayout springLayout = new SpringLayout();
    JPanel centerPanel = new JPanel(springLayout);

    JLabel userNameLabel = new JLabel("用户名:");
    JTextField userTxt = new JTextField();

    JLabel pwdLabel = new JLabel("密码:");
    JPasswordField pwdField = new JPasswordField();

    JLabel phoneLabel = new JLabel("联系电话:");
    JTextField phoneTxt = new JTextField();

    JButton registerBtn = new JButton("注册");
    JButton resetBtn = new JButton("重置");

    RegisterHandler registerHandler;


    public RegisterView() {
        super("用户注册界面");
        registerHandler = new RegisterHandler(this);

        Container contentPane = getContentPane();

        nameLabel.setFont(new Font("华文行楷", Font.PLAIN, 40));
        Font centerFont = new Font("楷体", Font.PLAIN, 20);
        userNameLabel.setFont(centerFont);
        userTxt.setPreferredSize(new Dimension(200, 30));
        pwdLabel.setFont(centerFont);
        pwdField.setPreferredSize(new Dimension(200, 30));
        phoneLabel.setFont(centerFont);
        phoneTxt.setPreferredSize(new Dimension(200, 30));

        resetBtn.setFont(centerFont);
        registerBtn.setFont(centerFont);

        //把组件加入面板
        centerPanel.add(userNameLabel);
        centerPanel.add(userTxt);
        centerPanel.add(pwdLabel);
        centerPanel.add(pwdField);
        centerPanel.add(phoneLabel);
        centerPanel.add(phoneTxt);

        //增加按键事件
        resetBtn.addActionListener(registerHandler);
        centerPanel.add(resetBtn);
        registerBtn.addActionListener(registerHandler);
        centerPanel.add(registerBtn);

        //弹簧布局
        layourCenter();


        contentPane.add(nameLabel, BorderLayout.NORTH);
        contentPane.add(centerPanel, BorderLayout.CENTER);

        //界面大小
        setSize(600, 400);
        setLocationRelativeTo(null);
        //关闭即为退出程序
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //设置不可改变大小
        setResizable(false);
        //设置可见
        setVisible(true);

        //设置registerBtn为默认按钮
        getRootPane().setDefaultButton(registerBtn);

    }
    //

    private void layourCenter() {
        //布局userNameLabel
        Spring childWidth = Spring.sum(Spring.sum(Spring.width(userNameLabel), Spring.width(userTxt)), Spring.constant(20));
        int offsetx = childWidth.getValue() / 2;
        springLayout.putConstraint(SpringLayout.WEST, userNameLabel, -offsetx, SpringLayout.HORIZONTAL_CENTER, centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH, userNameLabel, 20, SpringLayout.NORTH, centerPanel);

        //布局userTxt
        springLayout.putConstraint(SpringLayout.WEST, userTxt, 20, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, userTxt, 0, SpringLayout.NORTH, userNameLabel);

        //布局pwdLabel
        springLayout.putConstraint(SpringLayout.EAST, pwdLabel, 0, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pwdLabel, 20, SpringLayout.SOUTH, userNameLabel);

        //布局pwdField
        springLayout.putConstraint(SpringLayout.WEST, pwdField, 20, SpringLayout.EAST, pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pwdField, 0, SpringLayout.NORTH, pwdLabel);


        //布局phoneLabel
        springLayout.putConstraint(SpringLayout.EAST, phoneLabel, 0, SpringLayout.EAST, pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH, phoneLabel, 20, SpringLayout.SOUTH, pwdLabel);

        //布局phoneTxt
        springLayout.putConstraint(SpringLayout.WEST, phoneTxt, 20, SpringLayout.EAST, phoneLabel);
        springLayout.putConstraint(SpringLayout.NORTH, phoneTxt, 0, SpringLayout.NORTH, phoneLabel);


        //布局registerBtn
        springLayout.putConstraint(SpringLayout.WEST, registerBtn, 50, SpringLayout.WEST, phoneLabel);
        springLayout.putConstraint(SpringLayout.NORTH, registerBtn, 100, SpringLayout.SOUTH, phoneLabel);

        //布局resetBtn
        springLayout.putConstraint(SpringLayout.WEST, resetBtn, 50, SpringLayout.EAST, registerBtn);
        springLayout.putConstraint(SpringLayout.NORTH, resetBtn, 0, SpringLayout.NORTH, registerBtn);


    }


    public JTextField getUserTxt() {
        return userTxt;
    }

    public JPasswordField getPwdField() {
        return pwdField;
    }

    public JTextField getPhoneTxt() {
        return phoneTxt;
    }


}
